package com.atguigu.common.util;

import cn.hutool.core.date.LocalDateTimeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@Component
public class DateUtil {

    @Value("${mock.date}")
    private String mockDate;

    public LocalDateTime firstDay(Integer startHour) {
        return LocalDate.parse(mockDate).atStartOfDay().plusHours(startHour);
    }

    public LocalDateTime newDay(LocalDateTime yesterday, Integer startHour) {
        return yesterday.toLocalDate().plusDays(1).atTime(startHour, 0, 0);
    }

    public LocalDateTime later(LocalDateTime start, long ms) {
        return start.plus(ms, ChronoUnit.MILLIS);
    }
    public LocalDateTime before(LocalDateTime start, long ms) {
        return start.minus(ms, ChronoUnit.MILLIS);
    }

    public boolean longEnough(LocalDateTime start, LocalDateTime end, long ms) {
        return end.isAfter(later(start, ms));
    }

    public LocalDate toLocalDate(Date date) {
        return LocalDateTimeUtil.of(date).toLocalDate();
    }
}
